package cn.iocoder.yudao.module.member.user.controller.app;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.validation.Mobile;
import cn.iocoder.yudao.module.member.user.dal.dataobject.MemberUserDO;
import cn.iocoder.yudao.module.member.user.service.MemberRegisterService;
import cn.iocoder.yudao.module.member.user.service.MemberUserService;
import cn.iocoder.yudao.module.member.user.vo.AppAuthLoginRespVO;
import cn.iocoder.yudao.module.member.user.vo.AppAuthRegisterReqVO;
import cn.iocoder.yudao.module.member.user.vo.AppAuthRegisterReqVOV2;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  FIXME 新增注册功能
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/11/29
 */
@Tag(name = "用户 APP - 认证")
@RestController
@RequestMapping("/member/auth")
@Validated
@Slf4j
public class AppMemberRegisterController {
    @Resource
    private MemberRegisterService memberRegisterService;
    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/register")
    @Operation(summary = "注册", description = "已废弃，使用“手机号码+短信验证码”接口代替", deprecated = true)
    @PermitAll
    @Deprecated
    public CommonResult<AppAuthLoginRespVO> register(@Valid @RequestBody AppAuthRegisterReqVO reqVO) {
        return success(memberRegisterService.register(reqVO));
    }

    @GetMapping("/exists-mobile")
    @Operation(summary = "检查手机号是否已被占用")
    @Parameter(name = "mobile", description = "手机号", required = true, example = "15285116029")
    @PermitAll
    public CommonResult<Boolean> existsMobile(@RequestParam("mobile") @Mobile String mobile) {
        MemberUserDO user = memberUserService.getUserByMobile(mobile);
        return success(user != null);
    }

    @PostMapping("/registerV2")
    @Operation(summary = "注册")
    @PermitAll
    public CommonResult<UserSimpleVO> register(@RequestBody @Valid AppAuthRegisterReqVOV2 reqVO) {
        MemberUserDO user = memberRegisterService.register(reqVO);
        return success(BeanUtils.toBean(user, UserSimpleVO.class));
    }

}
